<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-07-26 14:10:20
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-09-04 11:30:25
 * @Description:
 *
-->

<template>
  <el-dialog
    class="create-dialog"
    title="创建集群"
    :visible="show"
    width="35vw"
    top="15vh"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="closeClick"
  >
    <el-form
      ref="form"
      class="create-form"
      :rules="rules"
      :model="form"
      label-width="auto"
      size="medium"
    >
      <el-form-item label="云服务商" prop="cloudProvider">
        <el-select
          v-model="form.cloudProvider"
          placeholder="请选择云服务商"
          style="width: 100%"
        >
          <el-option
            v-for="item in cloudProviderOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="云账号" prop="cloudAccountName">
        <el-select
          v-model="form.cloudAccountName"
          placeholder="请选择云账户"
          style="width: 100%"
          no-data-text="请选择合适的云服务商"
          :loading="loadingCloudAccountName"
        >
          <el-option
            v-for="item in cloudAccountNameOptions"
            :key="item.cloudAccountName"
            :label="item.cloudAccountName"
            :value="item.cloudAccountName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作系统" prop="os">
        <el-select
          v-model="form.os"
          placeholder="请选择操作系统"
          style="width: 100%"
        >
          <el-option
            v-for="item in osOptions"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="云主机" prop="vmId">
        <el-select
          v-model="form.vmId"
          placeholder="请选择云主机"
          style="width: 100%"
          :loading="loadingVMs"
        >
          <el-option
            v-for="item in vmOptions"
            :key="item.vmId"
            :label="item.vmName"
            :value="item.vmId"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="item" label="集群名称" prop="k8sClusterName">
        <el-input v-model="form.k8sClusterName" placeholder="请输入集群名称" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input
          v-model="form.desc"
          placeholder="请输入描述"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancelClick">取 消</el-button>
      <el-button
        type="primary"
        size="small"
        :loading="confirmLoading"
        @click="confirmClick"
      >确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import resSelectorMixin from '@/views/mixin/resSelector.js'
import { getVMListByOS } from '@/api/cn-resource'
import { vmAddK8sCluster } from '@/api/cnap/hybrid-cloud'
import debounce from 'lodash/debounce'

export default {
  name: 'CreateDialog',
  components: {},
  mixins: [resSelectorMixin],
  props: {
    // 是否显示
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      cloudProviderType: 'all',
      vmOptions: [],
      osOptions: ['Ubuntu 18.04'],
      form: {
        cloudProvider: '',
        cloudAccountName: '',
        os: 'Ubuntu 18.04',
        vmId: '',
        k8sClusterName: '',
        desc: ''
      },
      rules: {
        cloudProvider: [
          { required: true, message: '请选择云服务商', trigger: 'change' }
        ],
        cloudAccountName: [
          { required: true, message: '请选择云账户', trigger: 'change' }
        ],
        os: [{ required: true, message: '请选择操作系统', trigger: 'change' }],
        vmId: [{ required: true, message: '请选择云主机', trigger: 'change' }],
        k8sClusterName: [
          { required: true, message: '请输入集群名称', trigger: 'blur' }
        ]
      },
      loadingVMs: false,
      confirmLoading: false,
      vmQueryDebounce: null
    }
  },
  computed: {
    /**
     * @description: vm相关参数
     * @return {*}
     */
    vmParam() {
      return {
        cloudProvider: this.form.cloudProvider,
        cloudAccountName: this.form.cloudAccountName,
        os: this.form.os
      }
    }
  },
  watch: {
    vmParam: {
      handler(newVal) {
        this.form.vmId = ''
        if (!this.vmQueryDebounce) {
          // 创建防抖函数
          this.vmQueryDebounce = debounce(() => {
            if (
              this.form.cloudProvider &&
              this.form.cloudAccountName &&
              this.form.os
            ) {
              this.getVMList()
            }
          }, 300)
        }
        this.vmQueryDebounce()
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {},
  methods: {
    /**
     * @description: 获取云主机列表
     * @return {*}
     */
    getVMList() {
      this.loadingVMs = true
      getVMListByOS({
        cloudProvider: this.form.cloudProvider,
        cloudAccountName: this.form.cloudAccountName,
        os: this.form.os.split(' ')[0],
        osVersion: this.form.os.split(' ')[1]
      })
        .then((res) => {
          this.vmOptions = Array.isArray(res.data) ? res.data : []
        })
        .finally(() => {
          this.loadingVMs = false
        })
    },
    /**
     * @description: 确定
     * @return {*}
     */
    confirmClick() {
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          this.confirmLoading = true
          const vmFind = this.vmOptions.find((item) => {
            return item.vmId === this.form.vmId
          })
          if (!vmFind) {
            this.$message.warning('数据异常')
            return
          }
          vmAddK8sCluster({
            cloudVendor: this.form.cloudProvider,
            extranlIp: vmFind.publicIp,
            innerIp: vmFind.privateIp,
            k8sClusterName: this.form.k8sClusterName,
            osVersion: this.form.os,
            passwd: vmFind.pwd,
            regionId: vmFind.cpRegionId,
            vmName: this.form.vmId,
            zoneId: vmFind.cpZoneId
          })
            .then((res) => {
              this.$emit('success')
              this.closeClick()
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    /**
     * @description: 关闭
     * @return {*}
     */
    closeClick() {
      this.$emit('update:show', false)
    },
    /**
     * @description: 取消
     * @return {*}
     */
    cancelClick() {
      this.closeClick()
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.create-dialog {
  /deep/ .el-dialog {
    min-width: 600px;
  }
}
</style>
